<!--
 * @Author: zhangjingyu
 * @Date: 2021-03-29 10:49:48
 * @LastEditors: zhangjingyu
 * @LastEditTime: 2021-03-29 14:58:18
 * @FilePath: \vprisk10.14\src\views\ftpManage\pricingTrial\onlineConfiger\com\cashFlow.vue
 * @Description: 现金流表格
-->
<template>
  <div>
      <el-row   style="margin-bottom:10px;">
          <el-col>
              <el-button type="button" @click="exportCash">导出</el-button>
          </el-col>
      </el-row>
    <el-table :data="tableData">
      <el-table-column
        v-for="item in tableProps"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
      ></el-table-column>
    </el-table>
    <pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchForm.page"
      :page-size="searchForm.size"
      @refresh="searchFunc"
      :total="total"
    ></pagination>
  </div>
</template>

<script>
import { expricing } from "@/api/index";
export default {
  props: {
    param: { type: Object, default: () => {} },
    flag: { type: Boolean, default: () => false }
  },
  data() {
    return {
      // 现金流数据
      tableData: [],
      total: 0,
      searchForm: {
        page: 1,
        size: 15,
        acctNo: ""
      },
      tableProps: [
        { prop: "payDate", label: "支付日期" },
        { prop: "amount", label: "本金流" },
        { prop: "curBal", label: "当前余额" },
        { prop: "discountFacator", label: "折现因子" },
        { prop: "interest", label: "利息流" },
        { prop: "interestDay", label: "利息计算天数" },
        { prop: "pv", label: "现值" },
        { prop: "pvt", label: "现金流期限" },
        { prop: "pvtr", label: "现金流期限利率" },
        { prop: "pvxt", label: "当前总现值" },
        { prop: "rate", label: "插值利率" },
        { prop: "term", label: "插值天数" },
        { prop: "totalCashFlow", label: "本息和" }
      ]
    };
  },
  created() {
    this.searchFunc();
  },
  watch: {
    flag() {
      if (this.flag) {
        this.searchForm.page = 1;
        this.searchFunc();
      }
    }
  },
  methods: {
    searchFunc() {
      let data = this.searchForm;
      data.acctNo = this.param.acctNo;
      data.dataDt = this.param.dataDt;
      data.tableName = this.param.tableName;
      expricing.showCashFlow(data).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.rows;
          this.total = res.data.total;
        }
      });
    },
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.searchFunc();
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.searchFunc();
    },
    exportCash(){
        let data = this.$clone(this.param);
        expricing.expExpricingResult(data).then(res=>{
            console.log(res);
          if (res.status == 200) {
            console.log(decodeURI(res.headers["content-disposition"]));
            var filename = decodeURI(res.headers["content-disposition"]).split(
              "="
            )[1];
            let contenttype =
              filename.substring(filename.lastIndexOf("."), filename.length) ===
              "xlsx"
                ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
                : "application/vnd.ms-excel;charset=utf-8";

            var blob = new Blob([res.data], {
              type: contenttype,
            });
            if (window.navigator.msSaveOrOpenBlob) {
              navigator.msSaveBlob(blob, filename);
            } else {
              var downloadElement = document.createElement("a");
              var href = window.URL.createObjectURL(blob); //创建下载的链接
              downloadElement.href = href;
              downloadElement.download = filename; //下载后文件名
              document.body.appendChild(downloadElement);
              downloadElement.click(); //点击下载
              document.body.removeChild(downloadElement); //下载完成移除元素
              window.URL.revokeObjectURL(href); //释放掉blob对象
            }
            this.$message.success("导出成功");
          } else {
            this.$message.error(res.message || "获取失败");
          }
        })
        .catch(() => {});
        
    }
  }
};
</script>

<style lang="scss" scoped>
</style>